﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rabbit.ErCode;
using Rabbit.Write.API.WriteRepository.Commder;

namespace Rabbit.Write.API.Controllers
{
    /// <summary>
    /// Write—写的方法
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WriteTenController : ControllerBase
    {
        private readonly IMediator mediator;


        public WriteTenController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="SpinLock"></param>
        /// <returns></returns>
        [HttpPost]
        public async  Task<APIResult<string>> AddMer(AddMerchanCommder SpinLock)
        {
            return await mediator.Send(SpinLock);
        }



        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Image(IFormFile file)
        {
            var str = Guid.NewGuid();
            string p = Directory.GetCurrentDirectory() + @"\wwwroot\Uploads\" + str + file.FileName;
            using (FileStream fs = new FileStream(p, FileMode.OpenOrCreate))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return Ok("http://localhost:5211/Uploads/" + str + file.FileName);
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="delete"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<object>> DeleteMer([FromQuery] DeleteMerchanCommder delete)
        {
            return await mediator.Send(delete);
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<object>> SpSeletM([FromQuery] SpDeleteCommdes sp)
        {
            return await mediator.Send(sp);
        }

        /// <summary>
        /// 修改商品数据
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<object>> UpdateMen(UpdateMerchanCommd update)
        {
            return await mediator.Send(update);
        }

    } 
}
